//
//  EmbeddedScrollPage.m
//  ColumnWidget
//
//  Created by -_Asuna on 16/8/8.
//  Copyright © 2016年 David. All rights reserved.
//

#import "EmbenddedScrollPage.h"

#define TOP_INSET    40
#define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width

@interface EmbenddedScrollPage ()

@end

@implementation EmbenddedScrollPage{
    NSUInteger _currentIndex;
    UIView *_currentView;
    UIView *_newView;
    UIView *_lastView;
    CGFloat _firstLocation;
    CGFloat _lastLocation;
    NSUInteger _direction;
}


-(void)EmbendView{
    for(int index=0;index<10;index++){
        UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background"]];
        [_embenddedViews addObject:imageView];
        CGRect frame=CGRectMake(0, 0, _fatherScroll.bounds.size.width, _fatherScroll.bounds.size.height);
        frame.size.height-=_fatherScroll.contentInset.top;
        frame.origin.x=index*[UIScreen mainScreen].bounds.size.width;
        [imageView setFrame:frame];
        [_fatherScroll addSubview:imageView];
    }
}


-(void)setScrollInset:(UIEdgeInsets)insets{
    [_fatherScroll setContentInset:insets];
}
//-(void)setEmbenddedViews:(NSArray *)embenddedViews{
//    _embenddedViews=embenddedViews;
////    CGSize size=_fatherScroll.contentSize;
////    size.width=_embenddedViews.count*[UIScreen mainScreen].bounds.size.width;
////    [_fatherScroll setContentSize:size];
//  //   [self EmbendView];
//   // _reuseQueue=[NSSet setWithArray:_embenddedViews];
//}

#pragma mark - UIViewController methods

-(instancetype)init{
    self =[super init];
    if(self){
        _currentIndex=0;
        _direction=0;
        _fatherScroll =[[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [_fatherScroll setContentOffset:CGPointMake(0, -TOP_INSET)];
        [_fatherScroll setShowsHorizontalScrollIndicator:NO];
        _fatherScroll.pagingEnabled=YES;
        CGSize contentSize=CGSizeMake(SCREEN_WIDTH, 0);
        [_fatherScroll setContentSize:contentSize];
        _reuseQueue=[NSMutableSet set];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
        self.view=_fatherScroll;
    [self embenddedViews];
    [self loadFirtsView];
}


#pragma mark -Data Operation
-(void)loadFirtsView{
    _currentView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _fatherScroll.bounds.size.height)];
    UIView *contentView=[self.delegate askNewViewForIndex:0];
    [contentView setFrame:_currentView.bounds];
    [_currentView addSubview:contentView];
     [_fatherScroll addSubview:_currentView];
    CGSize contentSize=_fatherScroll.contentSize;
    contentSize.width+=contentSize.width;
    [_fatherScroll setContentSize:contentSize];
    _fatherScroll.delegate=self;
}

-(void)loadNextViewWithIndex:(NSUInteger)index{
    if(_reuseQueue.count){
        _newView=[_reuseQueue anyObject];
        [_reuseQueue removeObject:_newView];
    }else {
        _newView=[self.delegate askNewViewForIndex:0];
    }
    [_newView setFrame:CGRectMake(index*SCREEN_WIDTH, 0, SCREEN_WIDTH, _fatherScroll.bounds.size.height)];
    [_embenddedViews[index] addSubview:_newView];
}



#pragma mark- UIScorllViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(!_direction){
    if(!_firstLocation){
        _firstLocation=_fatherScroll.contentOffset.x;
        return ;
    }
    _lastLocation=_fatherScroll.contentOffset.x;
    if(_lastLocation>_firstLocation){
        _direction=1;
         [self loadNextViewWithIndex:_currentIndex+1];
    }else{
        _direction=2;
         [self loadNextViewWithIndex:_currentIndex-1];
    }
    _firstLocation=0;
    }
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
//    if(_direction==1){
//       
//    }else if (_direction==2){
//        [self loadNextViewWithIndex:_currentIndex-1];
//    }
//        _direction=0;
//    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if(_fatherScroll.contentOffset.x!=_currentIndex*SCREEN_WIDTH){
        if(_fatherScroll.contentOffset.x==(_currentIndex+1)*SCREEN_WIDTH){
            _currentIndex++;
        }else if(_fatherScroll.contentOffset.x==(_currentIndex-1)*SCREEN_WIDTH){
            _currentIndex--;
        }
        _lastView=_currentView;
        _currentView=_newView;
        [_lastView removeFromSuperview];
    }
    _direction=0;
}



















//#pragma mark - UIScrollViewPage DIY
//
//-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    if(_startScrollOffset==-1){
//        _startScrollOffset=[_fatherScroll contentOffset].x;
//    }
//}
//
//-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
//     [scrollView setContentOffset:_targetOffset animated:YES];
//    
//}
//
//-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
//   
//    if(targetContentOffset->x-_startScrollOffset>=_fatherScroll.frame.size.width/2){
//        //向左划
//        CGPoint offset=[_fatherScroll contentOffset];
//        offset.x=_startScrollOffset+_fatherScroll.frame.size.width;
//            [_fatherScroll setContentOffset:offset animated:YES];
//            _targetOffset=offset;
//        
//    }else if(_startScrollOffset-targetContentOffset->x>=_fatherScroll.frame.size.width/2){
//        //向右划
//       
//            CGPoint offset=[_fatherScroll contentOffset];
//            offset.x=_startScrollOffset-_fatherScroll.frame.size.width;
//            [_fatherScroll setContentOffset:offset animated:YES];
//            _targetOffset=offset;
//    }else {
//        //不够
//        CGPoint offset=[_fatherScroll contentOffset];
//        offset.x=_startScrollOffset;
//        [_fatherScroll setContentOffset:offset animated:YES];
//        _targetOffset=_fatherScroll.contentOffset;
//        _targetOffset.x=_startScrollOffset;
//    }
//    
//    _startScrollOffset=-1;
//    
//}

@end
